"""认证相关的请求和响应模型"""

from typing import Literal, Optional

from pydantic import BaseModel, EmailStr, Field, validator


class LoginRequest(BaseModel):
    """通用登录请求"""

    account: str = Field(..., description="账号")
    provider: Literal["password", "email", "phone"] = Field(..., description="认证方式")
    password: Optional[str] = Field(None, min_length=6, max_length=32, description="密码")
    code: Optional[str] = Field(None, min_length=6, max_length=6, description="验证码")
    captcha_id: Optional[str] = Field(None, description="图形验证码ID")
    captcha_code: Optional[str] = Field(None, description="图形验证码")


class RegisterRequest(BaseModel):
    """通用注册请求"""

    account: str = Field(..., description="账号")
    provider: Literal["password", "email", "phone"] = Field(..., description="认证方式")
    password: Optional[str] = Field(None, min_length=6, max_length=32, description="密码")
    code: Optional[str] = Field(None, min_length=6, max_length=6, description="验证码")
    captcha_id: Optional[str] = Field(None, description="图形验证码ID")
    captcha_code: Optional[str] = Field(None, description="图形验证码")


class ResetPasswordRequest(BaseModel):
    """重置密码请求"""

    account: str = Field(..., description="账号")
    provider: Literal["email", "phone"] = Field(..., description="认证方式")
    new_password: str = Field(..., min_length=6, max_length=32, description="新密码")
    code: str = Field(..., min_length=6, max_length=6, description="验证码")


class ChangePasswordRequest(BaseModel):
    """修改密码请求"""

    old_password: str = Field(..., description="旧密码")
    new_password: str = Field(..., min_length=6, max_length=32, description="新密码")


class BindAccountRequest(BaseModel):
    """绑定账号请求"""

    provider: Literal["password", "email", "phone"] = Field(..., description="认证方式")
    identifier: str = Field(..., description="认证账号")
    code: Optional[str] = Field(None, min_length=6, max_length=6, description="验证码")
    credential: Optional[str] = Field(None, min_length=6, max_length=32, description="认证凭证")


class UnbindAccountRequest(BaseModel):
    """解绑账号请求"""

    provider: Literal["password", "email", "phone"] = Field(..., description="认证方式")


class SendCodeRequest(BaseModel):
    """发送验证码请求"""

    account: str = Field(..., description="账号")
    provider: Literal["email", "phone"] = Field(..., description="认证方式")


class CaptchaResponse(BaseModel):
    """验证码响应"""

    captcha_id: str = Field(..., description="验证码ID")
    captcha_image: str = Field(..., description="验证码图片(base64)")


class TokenResponse(BaseModel):
    """令牌响应"""

    access_token: str = Field(..., description="访问令牌")
    refresh_token: str = Field(..., description="刷新令牌")
    token_type: str = Field("bearer", description="令牌类型")


class UserInfoResponse(BaseModel):
    """用户信息响应"""

    user_id: int = Field(..., description="用户ID")

    nickname: str = Field(..., description="用户名")
    avatar: str = Field(..., description="头像")

    menus: dict[int, dict] = Field(..., description="菜单")
    permissions: list[str] = Field(..., description="权限")
